#博客列表页自动化测试
from time import sleep

from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from BologAutoTest.Common.Utils import BlogDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from BologAutoTest.EdgeBrowser import BlogLoginTest


class BlogList:
    url=""
    driver =""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #登录情况
    def ListLoginSuccess(self):
        #查找狗头头像看是否存在判断是否成在博客列表页
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > img")
        #截图--1
        BlogDriver.getScreeShot()

        #点击主页并截图
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(4)").click()
        sleep(1)
        # 截图--2
        BlogDriver.getScreeShot()
        #返回上一页
        self.driver.back()

        #点击写博客并截图
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        sleep(5)
        # 截图--3
        BlogDriver.getScreeShot()
        #使用 self.driver.back()返回的是登录页面，说明历史记录被登录页污染
        #所以这里直接get回到列表页
        self.driver.get("http://8.137.19.140:9090/blog_list.html")
        sleep(5)

        # 检查博客的标题是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(2) > div.title")
        # 检查发布博客的时间是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(2) > div.date")
        #检查用户基本信息的昵称是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        # 检查用户基本信息的文章是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        # 检查用户基本信息的分类是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")

        #判断列表内是否有博客文章，有就测试点击是否能查看全文并截图
        if self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)"):
            self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a").click()
        # 截图--4
        BlogDriver.getScreeShot()
        sleep(1)
        # 返回上一页
        self.driver.back()

        # 点击GitHub并截图
        WebDriverWait(self.driver, 20).until(
            EC.element_to_be_clickable(
                (By.XPATH, "//a[@href='https://gitee.com/bubble-fish666/class-java45']")
            )
        ).click()

        # 等GitHub页面加载完成并检查特定元素是否存在
        try:
            WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located(
                    (By.CSS_SELECTOR,
                     "body > div.site-content > div.git-project-header > div.ui.container > div > div > h2 > span.project-title > a.author")
                )
            )
            print("页面加载完成，找到了指定元素")
            # 截图--5
            BlogDriver.getScreeShot()
        except TimeoutException:
            print("超时：页面未加载完成或未找到指定元素")
        self.driver.get("http://8.137.19.140:9090/blog_list.html")

        #查找狗头头像看是否存在，判断是否回到博客列表页
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > img")
        BlogDriver.getScreeShot()
        sleep(1)

    def QuitBlogLoginList(self):
        # 点击注销并截图
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()

        # 判断一下退出后的页面url和页面的标题
        Quittitle = self.driver.title
        QuitUrl = self.driver.current_url
        print(Quittitle)
        print(QuitUrl)
        assert Quittitle == "博客登陆页"
        assert QuitUrl == "http://8.137.19.140:9090/blog_login.html"
        # 判断一下账号输入框和密码输入框的内容是否为空
        Quitusername = self.driver.find_element(By.CSS_SELECTOR, "#username").text
        Quitpassword = self.driver.find_element(By.CSS_SELECTOR, "#password").text
        print("username为空" + Quitusername)
        print("password为空" + Quitpassword)
        assert Quitusername == ""
        assert Quitpassword == ""
        # 截图--1
        BlogDriver.getScreeShot()
        sleep(1)

#未登录情况
    def ListNotLogin(self):
        self.driver.get("http://8.137.19.140:9090/blog_list.html")
        # 截图--1
        BlogDriver.getScreeShot()
        try:
            element = WebDriverWait(self.driver,10).until(
                #EC.presence_of_element_located(...)指定等待条件：直到某个元素出现在 DOM 中。
                #DOM（Document Object Model，文档对象模型）是 一个表示和操作文档的标准编程接口。
                #它将文档（如 HTML 或 XML）视为一个由 节点和对象 组成的树形结构，每个节点代表文档的一部分（如元素、属性、文本等）。
                #通过 DOM，你可以用编程方式访问和修改文档的内容、结构和样式。
                EC.presence_of_element_located(
                    (By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
                )
            )
            print("页面加载完成，找到了指定元素")
        except TimeoutException:
            print("超时：页面未加载完成或未找到指定元素")
